package com.wbp.cms.qatest.service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

@Service
public class BServiceImpl implements BService ,ApplicationContextAware {

	 private ApplicationContext context; 
	 private BService proxy ;
	 
	 @Resource
	 private TransactionTemplate transactionTemplate ;
	
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void test1() {
      System.out.println("----test1-------:"+this.getClass().hashCode());
      proxy.test2();
      this.test3(); 
      proxy.test4();
      this.test5();
	}

	@Transactional(propagation=Propagation.REQUIRES_NEW)
	@Override
	public void test2() {
		System.out.println("----test2-------:"+this.getClass().hashCode());
		this.test3(); 
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		 this.context = applicationContext ;
		 System.out.println("----------setApplicationContext---------");
	}
	
	 @PostConstruct  //③ 初始化方法  
	    private void setSelf() {  
	        //从上下文获取代理对象（如果通过proxtSelf=this是不对的，this是目标对象）  
	        //此种方法不适合于prototype Bean，因为每次getBean返回一个新的Bean  
	        proxy = context.getBean(BService.class);   
	        System.out.println("--------setSelf------:"+proxy);
	        
	    }

	@Override
	public void test3() {
		transactionTemplate.setName(getClass()+".test3");
		transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); 
		transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
		//transactionTemplate.setTimeout(300);
		transactionTemplate.execute(new TransactionCallback<String>() {

			@Override
			public String doInTransaction(TransactionStatus status) {
				System.out.println("--------------test3------------------");
				status.setRollbackOnly();
				return null;
			}
		}) ;
		
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void test4() {
		 System.out.println("-----------test4----------");
	}

	@Override
	public void test5() {
		transactionTemplate.setName(getClass()+".test5");
		transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); 
		transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		//transactionTemplate.setTimeout(300);
		transactionTemplate.execute(new TransactionCallback<String>() {

			@Override
			public String doInTransaction(TransactionStatus status) {
				System.out.println("--------------test5------------------");
				//status.setRollbackOnly();
				return null;
			}
		}) ;
	}  

}
